import { JSEncrypt } from "jsencrypt";

const encryptor = new JSEncrypt();
// 生成RSA密钥对
const keypair = encryptor.getKey();
// 获取公钥和私钥
const publicKey = keypair.getPublicKey();
const privateKey = keypair.getPrivateKey();

const rsaEncrypt = function (data: string, key: string) {
  encryptor.setPublicKey(key ? key : publicKey);
  return encryptor.encrypt(data);
};

const rsaDecrypt = function (data: string, key: string) {
  encryptor.setPrivateKey(key ? key : privateKey);
  return encryptor.decrypt(data);
};

export { rsaEncrypt, rsaDecrypt };
